package com.yaoayao.mall.order.consumer;

import com.yaoayao.mall.order.service.OrderInfoService;
import com.yaoayao.mall.payment.client.PaymentFeignClient;
import com.yaoayao.mall.rabbit.constant.MqConst;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: 李旭
 * @Time: 11:20
 * @Description:
 */
@Component
public class CancelOrderConsumer {


    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private PaymentFeignClient paymentFeignClient;



    //取消订单
    @RabbitListener(queues = MqConst.QUEUE_ORDER_CANCEL)
    public void cancelOrder(Long orderId, Message message, Channel channel){
        try {

            //取消订单
            orderInfoService.cancelOrder(orderId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //关闭支付宝
    @RabbitListener(queues = MqConst.QUEUE_PAYMENT_CLOSE)
    public void closePayment(Long orderId, Message message, Channel channel){
        try {
            //关闭支付宝
            paymentFeignClient.closePayment(orderId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
